<?php

class Crud_Navigation extends Zend_Navigation
{
	protected $_module = 'admin';
	protected $_controller = 'crud';
	
    /**
     * Creates a new crud navigation container
     *
     * @param array|Zend_Config $pages    [optional] pages to add
     * @throws Zend_Navigation_Exception  if $pages is invalid
     */
    public function __construct()
    {
		$mainPage = new Zend_Navigation_Page_Mvc();
		$mainPage->setModule($this->_module);
		$mainPage->setController($this->_controller);
		$mainPage->setAction('index');

		$mainPage->setLabel('Главное меню');
		$this->addPage($mainPage);

    	$config = Crud_Config::getInstance()->getConfig();
    	$tables = array_keys($config->toArray());
		foreach ($tables as $table) {
			$page = new Zend_Navigation_Page_Mvc();
			$page->setModule($this->_module);
			$page->setController($this->_controller);
			$page->setAction('list');
			$page->setParams(array('table' => $table));

			$pageAdd = clone $page;
			$pageAdd->setAction('add');
			$pageAdd->setLabel('Добавление');

			$pageEdit = clone $page;
			$pageEdit->setVisible(false);
			$pageEdit->setAction('edit');
			$pageEdit->setLabel('Редактирование');

			$page->addPages(array($pageAdd, $pageEdit));

			$label = $config->$table->label ? $config->$table->label : $table;
			$page->setLabel($label);
			//$page->setConfig($config->$table);

			$mainPage->addPage($page);
		}
    }
    
}